<?php
namespace App\Model;

use App\Repository\Sys\Db;
use App\Repository\Common\ConstTable;
use App\Repository\Sys\JsonRespose;
use App\Repository\Sys\Str;

class FieldTable
{
    use Db;
    static private $table = 'field';


    static public function getFieldInfo($field_id)
    {

        $res = self::ORM()->where('id', $field_id)->first();
        $store_res = StorefrontTable::ORM()->where('id', $res->storefront_id)->first();
        return [
            'storefront_name' => $store_res->store_name,
            'type_text' => Str::getTypeText($res->type),
            'field_id' => $field_id,
            'field_type' => $res->type,
            'storefront_id' => $res->storefront_id,
            'img' => $store_res->img,
            'number' => $res->number,  //场地号
            'phone' => $store_res->phone
        ];
    }

    //获取是否预定锁定等状态
    static public function getFieldStatus($order_id, $lock)
    {

        $status = 0;
        if ($order_id != 0) {
            //已预定
            $status = 2;
        }
        if ($order_id == 0 && $lock == 0) {
            //空位
            $status = 1;
        }
        if ($order_id == 0 && $lock == 1) {
            //已锁定
            $status = 3;
        }
        return $status;

    }

    static public function getList($page, $limit, $store_id, $type)
    {
        $param = [];
        $where = '1=1';
        if ($store_id) {
            $where .= ' and storefront_id like :storefront_id';
            $param['storefront_id'] = $store_id;
        }

        if ($type) {
            $where .= ' and type like :type';
            $param['type'] = $type;
        }


        $count = \DB::select("select count(*) as c  from field WHERE {$where}", $param)[0]->c;
        $param['offset'] = ($page - 1) * $limit;
        $param['limit'] = $limit;
        $res = \DB::select("select * from field WHERE {$where} ORDER BY `number` asc limit :offset,:limit", $param);

        $list = [];
        foreach ($res as $v) {
            $v->type_text = Str::getTypeText($v->type);
            $list[] = $v;
        }
        $data = [
            'count' => $count,
            'list' => $list,
            'page' => $page,

        ];
        return JsonRespose::jsonArray(1, $data);
    }

//    static public function getProgram()
//    {
//        $res = FieldTable::ORM()->get();
//        $data = [];
//        foreach ($res as $v) {
//            $data[$v->id] = $v->program;
//        }
//        return $data;
//    }
}